package com.fare.taluo.application.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
  
public class MyMD5Util {  
      
    private static final String HEX_NUMS_STR="0123456789ABCDEF";  
    private static final Integer SALT_LENGTH = 12;  
      
    /**  
     * 将16进制字符串转换成字节数组  
     * @param hex  
     * @return  
     */  
    public static byte[] hexStringToByte(String hex) {  
        int len = (hex.length() / 2);  
        byte[] result = new byte[len];  
        char[] hexChars = hex.toCharArray();  
        for (int i = 0; i < len; i++) {  
            int pos = i * 2;  
            result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4   
                            | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));  
        }  
        return result;  
    }  
  
      
    /** 
     * 将指定byte数组转换成16进制字符串 
     * @param b 
     * @return 
     */  
    public static String byteToHexString(byte[] b) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < b.length; i++) {  
            String hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            hexString.append(hex.toUpperCase());  
        }  
        return hexString.toString();  
    }  
      
    /** 
     * 验证口令是否合法 
     * @param password 
     * @param passwordInDb 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws UnsupportedEncodingException 
     */  
    public static boolean validPassword(String password, String passwordInDb)  
            throws NoSuchAlgorithmException, UnsupportedEncodingException {  
        //将16进制字符串格式口令转换成字节数组  
        byte[] pwdInDb = hexStringToByte(passwordInDb);  
        //声明盐变量  
        byte[] salt = new byte[SALT_LENGTH];  
        //将盐从数据库中保存的口令字节数组中提取出来  
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);  
        //创建消息摘要对象  
        MessageDigest md = MessageDigest.getInstance("MD5");  
        //将盐数据传入消息摘要对象  
        md.update(salt);  
        //将口令的数据传给消息摘要对象  
        md.update(password.getBytes("UTF-8"));  
        //生成输入口令的消息摘要  
        byte[] digest = md.digest();  
        //声明一个保存数据库中口令消息摘要的变量  
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];  
        //取得数据库中口令的消息摘要  
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);  
        //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同  
        if (Arrays.equals(digest, digestInDb)) {  
            //口令正确返回口令匹配消息  
            return true;  
        } else {  
            //口令不正确返回口令不匹配消息  
            return false;  
        }  
    }  
  
  
    /** 
     * 获得加密后的16进制形式口令 
     * @param password 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws UnsupportedEncodingException 
     */  
    public static String getEncryptedPwd(String password)  
            throws NoSuchAlgorithmException, UnsupportedEncodingException {  
        //声明加密后的口令数组变量  
        byte[] pwd = null;  
        //随机数生成器  
        SecureRandom random = new SecureRandom();  
        //声明盐数组变量  
        byte[] salt = new byte[SALT_LENGTH];  
        //将随机数放入盐变量中  
        random.nextBytes(salt);  
  
        //声明消息摘要对象  
        MessageDigest md = null;  
        //创建消息摘要  
        md = MessageDigest.getInstance("MD5");  
        //将盐数据传入消息摘要对象  
        md.update(salt);  
        //将口令的数据传给消息摘要对象  
        md.update(password.getBytes("UTF-8"));  
        //获得消息摘要的字节数组  
        byte[] digest = md.digest();  
  
        //因为要在口令的字节数组中存放盐，所以加上盐的字节长度  
        pwd = new byte[digest.length + SALT_LENGTH];  
        //将盐的字节拷贝到生成的加密口令字节数组的前12个字节，以便在验证口令时取出盐  
        System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);  
        //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节  
        System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);  
        //将字节数组格式加密后的口令转化为16进制字符串格式的口令  
        return byteToHexString(pwd);  
    }  
    
    public static void main(String []args){
    	String str= "D19BC20CEB70C9685ABA4FCD2E5845ADDF42E70E3517B6FE44C8FCE92DD9B5B23CFB3E619F4BCFC64BE2CE49D7C67426652EB3567A98C163A306ACB500F3699423FD2E3A9CD2ED4CF1A1B83716DB2C81E00A8B0BC3245DA11078B58ADE8C75ABAD61F89F439C4E7B97439A9C4B75E6A1FEBCC3141976A4174E52C08DF345C6233CC3053E92B16A4BEE5538AD9D6596D6A749FD237CAD2D5C031633CDE751A4DB15DF3EA2D2619EEB6850886CC2D4EF7442BB301725B8915DDE3613B93F8AED058733E552A0A694E2A238C435709A272553BD700D42A722D41FDD327AAED9E1FB5CBC0D3BBA0924E9F6C112741F40F7CFCC508516752B0E3C9826508DCF95C459A9777ADEEC2480D34FAAB819D89A4D91F40ADCFB41D1D3DF5DB104BE64C65D14BB62195B7CC83B3020A71AFB7CD43F985152504960F87C1AAC6F322F26A3369972DB619D12D43242745F17F9B5AD510AD44CAB2833331BC605E3660C1C05932DC48236E2A951D07C843D74DE08364A8872D5174533565A5B7AB67E7137083AB92DED64674BAAAC955C8126EE807DC5726738E3DA6AAE2F60EAF14DFA172D63A4FA7D4DAE122DB4CF6EAA665AB99F4161AC5D297133F4102CB7C48E0F60B9445678FFC9141E7430C94F967A520FC01DEE9AF7911CF37B5FE6ECEE624B15B72BF493DCC69A41531AA6935F5193A8DF2F324E3B1D51FBC14385D4F0FBA188FF899F81C1102846C18AE95055393CC6DA07D88073FB9903C5864EB4DCC70E43D19EFFFC20C450330D5401FCC6BFA93B3D4405B433AF0E0A5671692D62789A96F73D0E5D329B1EE7382663A5C8555907098033241CAFC791C33839FD7B6A193A9B6A5393BFC9BA429AD7653213ADE1759517A229A0CAEBE136EA36582F4C5C7A94EE35F1E38A58A0ED9FE8DC4D2F6D4A5594D5595F487EC5C14930F1A68F4CC63879A9AB09B50959D9F18D56CDF991F721DA8EF89694098ED7D74F37191E7AB53E5C3C5307AFC8ED804357F87FD4C384787E4E55ECEFC5A4D1D8836E5B5F95CCCCF109A54290D6C807CF84B183652F8FF0AF22426A23A15C10DFC9C9FAF8BF35774D98168C9345852B6E985798DB6C9095903178BB73B053326CE69FF42C646A7ACA356FB67EFA37B6275BED864E7BE5E0B3CF080E9C934E9C826BCCBC60F5A3C3B437A70C2A7EB44C00974024AA8066E3BD91D84C0EAA737745B1F011C690184CDEC003560292A409E3339345F7BF7D063245541FE570B6291E94B3C48786396F763E50C7B210F5D385986B242E9A387CA42381687305621B957B445E500DA17689626386A1CE403F3514E9AD54812B5874DC74FDAFE7EC287A089EDE41CA4F391FB35219306580A1F9972EF9B25665E044D2954168E023A8D28F08DEB9E92E6933EB73A73DE3B0FF3656B4C02B5969ABEFF93928D83EECB47CF3C610D0F029A4B528D64B958772C23F16D109A7D335F3F7F2F4513E8AE3158F5E59B3CFF381E104B102EC4B330DAFCCB18AD443396DC6DF1B968A6EA40C539A4B01E58BFA04AA364D760705DCADA6EE56797351E4AC68CFE867887408920C737CACB6873BAF1F3BDDE3A4362E3EFD60DA3F1C06986B37B4CB7295972AF3DD3F175924502DAD2B5677037FB09E54AE397693617DA903F1283E3C7508D8D4F0DFE4D8754558F23B2670573155F7BA456E1A63BCBC9E8F8EC096C2E71865DDADB1AB5B2008BD02D2D9B4BDDC7A26135F9157912CC1FC952EC07A";
    	String str2="D19BC20CEB70C9685ABA4FCD2E5845ADDF42E70E3517B6FE44C8FCE92DD9B5B23CFB3E619F4BCFC64BE2CE49D7C67426652EB3567A98C163A306ACB500F36994A570739A8FF298395686606C0A325D65E00A8B0BC3245DA11078B58ADE8C75AB8AD2DA91E4A110D54DD6957AE672317AFEBCC3141976A4174E52C08DF345C6233CC3053E92B16A4BEE5538AD9D6596D6A749FD237CAD2D5C031633CDE751A4DB15DF3EA2D2619EEB6850886CC2D4EF7442BB301725B8915DDE3613B93F8AED058733E552A0A694E2A238C435709A272553BD700D42A722D41FDD327AAED9E1FB5CBC0D3BBA0924E9F6C112741F40F7CFCC508516752B0E3C9826508DCF95C459A9777ADEEC2480D34FAAB819D89A4D91F40ADCFB41D1D3DF5DB104BE64C65D14BB62195B7CC83B3020A71AFB7CD43F985152504960F87C1AAC6F322F26A3369972DB619D12D43242745F17F9B5AD510AD44CAB2833331BC605E3660C1C05932DC48236E2A951D07C843D74DE08364A8872D5174533565A5B7AB67E7137083AB92DED64674BAAAC955C8126EE807DC5726738E3DA6AAE2F60EAF14DFA172D63A4FA7D4DAE122DB4CF6EAA665AB99F4161AC5D297133F4102CB7C48E0F60B9445678FFC9141E7430C94F967A520FC01DEE9AF7911CF37B5FE6ECEE624B15B72BF493DCC69A41531AA6935F5193A8DF2F324E3B1D51FBC14385D4F0FBA188FF899F81C1102846C18AE95055393CC6DA07D88073FB9903C5864EB4DCC70E43D19EFFFC20C450330D5401FCC6BFA93B3D4405B433AF0E0A5671692D62789A96F73D0E5D329B1EE7382663A5C8555907098033241CAFC791C33839FD7B6A193A9B6A5393BFC9BA429AD7653213ADE1759517A229A0CAEBE136EA36582F4C5C7A94EE35F1E38A58A0ED9FE8DC4D2F6D4A5594D5595F487EC5C14930F1A68F4CC63879A9AB09B50959D9F18D56CDF991F721DA8EF89694098ED7D74F37191E7AB53E5C3C5307AFC8ED804357F87FD4C384787E4E55ECEFC5A4D1D8836E5B5F95CCCCF109A54290D6C807CF84B183652F8FF0AF22426A23A15C10DFC9C9FAF8BF35774D98168C9345852B6E985798DB6C9095903178BB73B053326CE69FF42C646A7ACA356FB67EFA37B6275BED864E7BE5E0B3CF080E9C934E9C826BCCBC60F5A3C3B437A70C2A7EB44C00974024AA8066E3BD91D84C0EAA737745B1F011C690184CDEC003560292A409E3339345F7BF7D063245541FE570B6291E94B3C48786396F763E50C7B210F5D385986B242E9A387CA42381687305621B957B445E500DA17689626386A1CE403F3514E9AD54812B5874DC74FDAFE7EC287A089EDE41CA4F391FB35219306580A1F9972EF9B25665E044D2954168E023A8D28F08DEB9E92E6933EB73A73DE3B0FF3656B4C02B5969ABEFF93928D83EECB47CF3C610D0F029A4B528D64B958772C23F16D109A7D335F3F7F2F4513E8AE3158F5E59B3CFF381E104B102EC4B330DAFCCB18AD443396DC6DF1B968A6EA40C539A4B01E58BFA04AA364D760705DCADA6EE56797351E4AC68CFE867887408920C737CACB6873BAF1F3BDDE3A4362E3EFD60DA3F1C06986B37B4CB7295972AF3DD3F175924502DAD2B5677037FB09E54AE397693617DA903F1283E3C7508D8D4F0DFE4D8754558F23B2670573155F7BA456E1A63BCBC9E8F8EC096C2E71865DDADB1AB5B2008BD02D2D9B4696EC45903B27750A5EE47FEF667DE6A";
    	try {
    		byte[] b=hexStringToByte(str);
    		byte[] b2=hexStringToByte(str2);
    		String s=javax.xml.bind.DatatypeConverter.printBase64Binary(b);
    		String s2=javax.xml.bind.DatatypeConverter.printBase64Binary(b2);
    		System.out.println(s);
    		System.out.println(s2);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }

}  

